layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 软风对账管理管理
     */
    var ToolExpressReconciliationRf = {
        tableId: "toolExpressReconciliationRfTable"
    };

    /**
     * 初始化表格的列
     */
    ToolExpressReconciliationRf.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'reconciliationMonth', sort: true, title: '对账月份'},
            {field: 'outNumber', sort: true, title: '出库单号'},
            {field: 'shopNumber', sort: true, title: '店铺编号'},
            {field: 'shopName', sort: true, title: '店铺名称'},
            {field: 'platform', sort: true, title: '平台站点'},
            {field: 'outWNumber', sort: true, title: '出仓单号'},
            {field: 'inOrderNumber', sort: true, title: '内部订单号'},
            {field: 'outTime', sort: true, title: '出库日期'},
            {field: 'states', sort: true, title: '状态'},
            {field: 'expressName', sort: true, title: '快递公司'},
            {field: 'orderNumber', sort: true, title: '快递单号'},
            {field: 'province', sort: true, title: '省份'},
            {field: 'remarks', sort: true, title: '备注'},
            {field: 'provinces', sort: true, title: '省'},
            {field: 'city', sort: true, title: '市'},
            {field: 'area', sort: true, title: '区'},
            {field: 'street', sort: true, title: '街道'},
            {field: 'nums', sort: true, title: '发货件数'},
            {field: 'chargingWeightKg', sort: true, title: '计费重量'},
            {field: 'roundingWeightKg', sort: true, title: '取整重量'},
            {field: 'expressMoney', sort: true, title: '快递费'},
            {field: 'unilateralism', sort: true, title: '面单费'},
            {field: 'cWeight', sort: true, title: '续重'},
            {field: 'standard', sort: true, title: '二三级中转标准'},
            {field: 'minimum', sort: true, title: '二三级中转最低'},
            {field: 'transferMoney', sort: true, title: '中转费'},
            {field: 'additional', sort: true, title: '地区加收'},
            {field: 'additionalE', sort: true, title: '疫情加收'},
            {field: 'additionalSpecial', sort: true, title: '特殊城市加收'},
            {field: 'additionalBusySeason', sort: true, title: '旺季加收'},
            {field: 'goBeyond', sort: true, title: '超区'},
            {field: 'sumExpressMoney', sort: true, title: '总快递费'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolExpressReconciliationRf.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ToolExpressReconciliationRf.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ToolExpressReconciliationRf.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/toolExpressReconciliationRf/add';
    };

    /**
     * 导出excel按钮
     */
    ToolExpressReconciliationRf.exportExcel = function () {
        var checkRows = table.checkStatus(ToolExpressReconciliationRf.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ToolExpressReconciliationRf.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/toolExpressReconciliationRf/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ToolExpressReconciliationRf.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/toolExpressReconciliationRf/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ToolExpressReconciliationRf.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolExpressReconciliationRf.tableId,
        url: Feng.ctxPath + '/toolExpressReconciliationRf/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: ToolExpressReconciliationRf.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolExpressReconciliationRf.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ToolExpressReconciliationRf.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolExpressReconciliationRf.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ToolExpressReconciliationRf.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ToolExpressReconciliationRf.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ToolExpressReconciliationRf.onDeleteItem(data);
        }
    });
});
